package org.msss.gamebasic;

import org.lwjgl.opengl.Display;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.msss.resource.IconDB;
import org.msss.resource.ImageDB;
import org.msss.resource.RoomDB;

/**
 * The frame, the name is pretty straight forward.
 * 
 * @author alanxoc3
 * 
 */
public final class Frame {

	public static final int WINDOW_WIDTH = 200;
	public static final int WINDOW_HEIGHT = 200;
	public static final int FRAMERATE = 30;

	/**
	 * Starts up the frame.
	 */
	public static void startFrame() {

		initFrame();
		
		ImageDB.init();
		IconDB.init();
		RoomDB.init();

		// Game loop
		while (!Display.isCloseRequested()) {
			updateFrame();
		}
	}

	/**
	 * Here we will set up the configuration for the frame.
	 */
	private static void initFrame() {

		try {
			Display.setDisplayMode(new DisplayMode(WINDOW_WIDTH, WINDOW_HEIGHT));
			Display.create();
		} catch (LWJGLException lwjgle) {
			lwjgle.printStackTrace();
		}
		
		GL11.glMatrixMode(GL11.GL_PROJECTION);
		GL11.glLoadIdentity();
		GL11.glOrtho(0, WINDOW_WIDTH, WINDOW_HEIGHT, 0, 1000, -1000);
		GL11.glMatrixMode(GL11.GL_MODELVIEW);

		GL11.glEnable(GL11.GL_DEPTH_TEST);
		GL11.glEnable(GL11.GL_LINEAR);
	}

	/**
	 * This is the what loops in the game loop, it will update the game.
	 */
	private static void updateFrame() {

		// Clear the screen of drawings
		GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);

		// Game Stuff goes here.
		Game.loop();

		// Update the drawing buffer
		Display.update();
		// Sync with fps to consume less resources.
		Display.sync(FRAMERATE);

	}

}
